/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import database.database;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import structure.Tree;

/**
 *
 * @author Erdiansyah
 */
public class RoomListHandler extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here*/
            database db = new database();
            db.createConnection();

            ResultSet rs;
            int minID = 0;
            int maxID = 0;
            int curID = 0;
            int printID;
            printID = minID;

            if (request.getParameter("id") != null) {
                printID = Integer.parseInt(request.getParameter("id"));
            }


            //Create Pohon
            Tree<Integer> categoryTree = null;
            db.select("SELECT * FROM categorydb ORDER BY categoryid ASC");
            rs = db.getResultSet();
            if (rs.next()) {
                rs.last();
                maxID = rs.getInt("categoryid");
            }

            categoryTree = new Tree<Integer>(minID);

            db.select("SELECT * FROM categorydb ORDER BY categoryid ASC");
            rs = db.getResultSet();
            while (rs.next()) {
                while (curID <= maxID) {
                    if (rs.getInt("parentid") == curID) {
                        categoryTree.addLeaf(curID, rs.getInt("categoryid"));
                    }
                    curID++;
                }
                curID = minID;
            }

            String category = "";
            if (printID != 0) {
                if (db.getParentID(printID) == 0) {
                    category += "<br><a href = 'javascript:;' onclick = 'javascript: document.getElementById(\"category\") .submit()' >Up One Category (Category)</a>";
                } else {
                    category += "<br><a href = 'javascript:;' onclick = 'javascript: document.getElementById(\"category\") .submit()' >Up One Category (" + db.getCategoryName(db.getParentID(printID)) + ")</a>";
                }
                category += "<br>" + db.getCategoryName(printID);
            }

            String subcategory = "";
            for (int node : categoryTree.getSuccessors(printID)) {
                if (!categoryTree.getSuccessors(node).isEmpty()) {
                    subcategory += "\n<a href = 'javascript:;' onclick = 'javascript: document.getElementById(\"category\") .submit()' >" + db.getCategoryName(node) + "</a>";
                } else {
                    subcategory += "\n" + db.getCategoryName(node) + "";
                }
            }

            out.print("<div>");out.print(category);out.print("<div>");
            out.print("<div>");out.print(subcategory);out.print("<div>");

            db.closeConnection();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RoomListHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(RoomListHandler.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
